home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / bbs / citsrc6K05.lha / netcall.c < prev    next >
C/C++ Source or Header  |  1996-10-27  |  30KB  |  1,194 lines

  1. /*
  2. *       netcall.c
  3. *
  4. * Networking call functions.
  5. */
  6. /*
  7. *       history
  8. *
  9. * 86Aug20 HAW  History not maintained due to space problems.
  10. */
  11. #include "ctdl.h"
  12. #include "math.h"
  13. /*
  14. *       contents
  15. *
  16. */
  17. char                    inReceive;
  18. NetInfo     NetStyle;
  19. extern char   RecMassTransfer;
  20. extern char             *SR_Sent;
  21. extern char             *pollCall;
  22. extern FILE             *netMisc;
  23. extern FILE             *netLog;
  24. extern AN_UNSIGNED      RecBuf[SECTSIZE + 5];
  25. extern int              counter;
  26. extern int              callSlot;
  27. extern label            callerName, callerId;
  28. char                    checkNegMail;
  29. extern char             processMail;
  30. extern char   MassTransferSent;
  31. char             normId(), getNetMessage();
  32. AN_UNSIGNED      inp();
  33. extern CONFIG    cfg;            /* Lots an lots of variables    */
  34. extern logBuffer logBuf;         /* Person buffer                */
  35. extern logBuffer logTmp;         /* Person buffer                */
  36. extern aRoom     roomBuf;        /* Room buffer                  */
  37. extern rTable    *roomTab;
  38. extern PROTO_TABLE Table[];
  39. extern int TransProtocol;
  40. extern MessageBuffer   msgBuf;
  41. extern NetBuffer netBuf, netTemp;
  42. extern NetTable  *netTab;
  43. /* extern SListBase FwdAliasii; */
  44. extern int       thisNet;
  45. extern char      onConsole;
  46. extern char      loggedIn;       /* Is we logged in?             */
  47. extern char      outFlag;        /* Output flag                  */
  48. extern char      haveCarrier;    /* Do we still got carrier?     */
  49. extern char      modStat;        /* Needed so we don't die       */
  50. extern char      TrError;
  51. extern int       thisRoom;
  52. extern char      netDebug;
  53. extern char      logNetResults;
  54. extern char  *DomainFlags;
  55. extern long ByteCount, EncCount;
  56. char *chMailTemplate = "chkMail.$$$";
  57. extern long  char_in, char_out; /* send/recieve character counts*/
  58. long start_time;                /* total time of session */
  59. /*
  60. * caller()
  61. *
  62. * This function is called when we've been called and have to handle the
  63. * caller.  We have to stabilize the call and then manage all requests
  64. * the caller makes of us.
  65. */
  66. void caller()
  67.   {
  68.   ITL_InitCall();             /* initialize the ITL layer */
  69.   RecMassTransfer = FALSE;
  70.   memset(SR_Sent, 0, SHARED_ROOMS);
  71.   inReceive = FALSE;
  72.   processMail = FALSE;
  73.   checkNegMail = FALSE;
  74.   SpecialMessage("Status:Net Carrier");
  75.   if( logNetResults && netDebug )splitF(netLog, "Have Carrier\n");
  76.   caller_stabilize();
  77.   if (!haveCarrier) return ;  /* Abort */
  78.   if( logNetResults )splitF(netLog, "Stabilized\n");
  79.   SpecialMessage("Status:Net Session");
  80.   char_in = char_out = 0;
  81.   start_time = Set_Timer(0);
  82.   sendId();
  83.   if (!haveCarrier) return ;  /* Abort */
  84.   if (!netBuf.nbflags.MassTransfer)
  85.   ITL_optimize(TRUE);   /* try for better protocol */
  86.   sendStuff(FALSE, TRUE);
  87.   startTimer(WORK_TIMER);
  88.   while (gotCarrier() && chkTimeSince(WORK_TIMER) < 10) ;
  89.   killConnection();
  90.   if( logNetResults )splitF(netLog, "\nFinished with %s @%s\n", netBuf.netName, Current_Time());
  91.   Compute_Data(netBuf.netName);
  92.   doResults();
  93.   SpecialMessage("Status:Net Completed");
  94.  
  95.   }
  96. /*
  97. * sendStuff()
  98. *
  99. * This function handles being the sender of information (sending role).
  100. */
  101. void sendStuff(char reversed, char SureDoIt)
  102.   {
  103.   extern int RouteToDirect;
  104.   if (SureDoIt && callSlot != ERROR)
  105.     {
  106.     MassTransferSent = FALSE;
  107.     SendPwd();
  108.     if (!haveCarrier) return ;  /* Abort */
  109.     if (netBuf.nbflags.HasRouted)    RouteOut();
  110.     if (!haveCarrier) return ;  /* Abort */
  111.     if (!netBuf.nbflags.Stadel) DomainOut(FALSE);
  112.     if (!haveCarrier) return ;  /* Abort */
  113.     if (netBuf.nbflags.normal_mail ||
  114.     RouteToDirect != -1 ||
  115.     (netBuf.nbflags.Stadel && DomainFlags[thisNet]))
  116.       {
  117.       sendMail();
  118.       if (!haveCarrier) return ;  /* Abort */
  119.       checkMail();
  120.  
  121.       }
  122.     if (!haveCarrier) return ;  /* Abort */
  123.     if (!HasPriorityMail(thisNet))
  124.       {
  125.       if (netBuf.nbflags.room_files)
  126.       askFiles();
  127.       if (!haveCarrier) return ;  /* Abort */
  128.       sendSharedRooms();
  129.       if (!haveCarrier) return ;  /* Abort */
  130.       if (netBuf.nbflags.send_files)
  131.       doSendFiles();
  132.       if (!haveCarrier) return ;  /* Abort */
  133.       roleReversal(reversed);
  134.  
  135.       }
  136.  
  137.     }
  138.   sendHangUp();
  139.   pollCall[thisNet]--;        /* Don't set polled flag unless stable call */
  140.  
  141.   }
  142. /*
  143. * SendPwd()
  144. *
  145. * This function sends the system password if necessary.
  146. */
  147. void SendPwd()
  148.   {
  149.   struct cmd_data cmds;
  150.   if (netBuf.TheirPwd[0] != 0)
  151.     {
  152.     /* only send if need to -- gets */
  153.     zero_struct(cmds);              /* us around a bug in pre net 1.10*/
  154.     strCpy(cmds.fields[0], netBuf.TheirPwd);        /* versions     */
  155.     cmds.command = SYS_NET_PWD;
  156.     sendNetCommand(&cmds, "system pwd");
  157.  
  158.     }
  159.  
  160.   }
  161. /*
  162. * roleReversal()
  163. *
  164. * This function handles the role reversal request.
  165. */
  166. void roleReversal(char reversed)
  167.   {
  168.   struct cmd_data cmds;
  169.   if (reversed) return ;
  170.   if (!netBuf.nbflags.local && !netBuf.nbflags.spine) return ;
  171.   if( logNetResults && netDebug )splitF(netLog, "Reversing roles\n");
  172.   zero_struct(cmds);
  173.   cmds.command = ROLE_REVERSAL;
  174.   if (!sendNetCommand(&cmds, "role reversal"))
  175.   return;
  176.   rcvStuff(TRUE);
  177.   pause(50);   /* wait a second */
  178.   if (gotCarrier())
  179.   reply(GOOD, "");/* this replies GOOD to the HANGUP terminating the */
  180.   /* role reversal.  NOTE: STadel doesn't follow the */
  181.   /* spec in this regard - it just dumps carrier     */
  182.  
  183.   }
  184. /*
  185. * caller_stabilize()
  186. *
  187. * This function tries to stabilize the call -- baud is already set.
  188. */
  189. void caller_stabilize()
  190.   {
  191.   int tries, x1, x2, x3;
  192.   extern char hst;
  193.   /* regrettable initialization */
  194.   for (tries = 0; tries < SHARED_ROOMS; tries++) resetNeedsProcessing(tries);
  195.   putNet(thisNet, &netBuf);
  196.   pause(50);                                  /* delay for a little bit */
  197.   while (MIReady())   inp();                  /* Clear garbage        */
  198.   startTimer(USER_TIMER);                     /* this is safe */
  199.   x1 = x2 = x3 = 0;
  200.   for (tries = 0; (chkTimeSince(USER_TIMER) < 20l || tries < 40) &&
  201.   gotCarrier() ; tries++)
  202.     {
  203.     outMod(7);
  204.     outMod(13);
  205.     outMod(69);
  206.     for (  startTimer(WORK_TIMER);
  207.     chkTimeSince(WORK_TIMER) < 2l && !MIReady();) ;
  208.     if (MIReady())
  209.       {
  210.       x1 = receive(2);
  211.       x2 = receive(2);
  212.       if (x2 != ERROR) x3 = receive(2);
  213.       if (x1 == 248 && x2 == 242 && x3 == 186)
  214.         {
  215.         outMod(ACK);
  216.         /* ok, we've seen at high speed some overrun problems, so ... */
  217.         do
  218.           {
  219.           x1 = receive(2);
  220.  
  221.           }
  222.         while (x1 == 248 || x1 == 242 || x1 == 186);
  223.         ModemPushBack(x1);
  224.         return;
  225.  
  226.         }
  227.       else
  228.         {
  229.         if ( ( x1 == 242 || x1 == 186 || x1 == 248 )
  230.         || ( x2 == 242 || x2 == 186 || x2 == 248 )
  231.         || ( x3 == 242 || x3 == 186 || x3 == 248 ) )  /* all combinations */
  232.           {
  233.           /* real close, so let's catch our breath */
  234.           while (receive(1) != ERROR) ;
  235.  
  236.           };
  237.  
  238.         }
  239.  
  240.       }
  241.     else
  242.       {
  243.       tries ++;  /* if we get nothing back, don't try as much, try half the number of times */
  244.  
  245.       }
  246.  
  247.     }
  248.   if( logNetResults && netDebug )splitF(netLog, "Call not stabilized:tries=%d \n",tries);
  249.   killConnection();
  250.  
  251.   }
  252. /*
  253. * sendId()
  254. *
  255. * This function sends ID to the receiver.
  256. */
  257. void sendId()
  258.   {
  259.   if (!ITL_Send(STARTUP))
  260.     {
  261.     no_good("Couldn't transfer ID to %s at startup!", TRUE);
  262.     return;
  263.  
  264.     }
  265.   mTrPrintf("%s", cfg.codeBuf + cfg.nodeId  );
  266.   mTrPrintf("%s", cfg.codeBuf + cfg.nodeName);
  267.   if (!ITL_Send(FINISH))
  268.     {
  269.     no_good("Couldn't transfer ID to %s at finish!", TRUE);
  270.     return;
  271.  
  272.     }
  273.  
  274.   }
  275. /*
  276. * sendMail()
  277. *
  278. * This function sends normal mail to receiver.
  279. */
  280. void sendMail()
  281.   {
  282.   struct cmd_data cmds;
  283.   int             nor_mail;
  284.   extern int      RouteToDirect;
  285.   if (!gotCarrier())
  286.     {
  287.     modStat = haveCarrier = FALSE;
  288.     return ;
  289.  
  290.     }
  291.   if( logNetResults )splitF(netLog, "Sending Mail ");
  292.   zero_struct(cmds);
  293.   cmds.command = NORMAL_MAIL;
  294.   if (!sendNetCommand(&cmds, "normal mail"))
  295.   return;
  296.   if (!ITL_SendMessages())
  297.     {
  298.     no_good("Couldn't start Mail transfer to %s!", TRUE);
  299.     killConnection();
  300.     return;
  301.  
  302.     }
  303.   nor_mail = s_m_n();            /* Send normal mail     */
  304.   if (gotCarrier())
  305.   nor_mail += SendRoutedAsLocal();
  306.   if (gotCarrier() && netBuf.nbflags.Stadel)
  307.   nor_mail += DomainOut(TRUE);
  308.   ITL_StopSendMessages();
  309.   RouteToDirect = -1;         /* This is just in case */
  310.   if (gotCarrier())
  311.     {
  312.     if( logNetResults )splitF(netLog, "(%d) (%ld => %ld bytes)\n",nor_mail,EncCount,ByteCount);
  313.     netBuf.nbflags.normal_mail = FALSE;
  314.  
  315.     }
  316.  
  317.   }
  318. /*
  319. * checkMail()
  320. *
  321. * This function handles negative acknowledgement on netMail.
  322. */
  323. void checkMail()
  324.   {
  325.   struct cmd_data cmds;
  326.   SYS_FILE fileNm;
  327.   extern char *WRITE_ANY;
  328.   if (!gotCarrier())
  329.     {
  330.     return;
  331.  
  332.     }
  333.   if( logNetResults && netDebug )splitF(netLog, "Check mail\n");
  334.   makeSysName(fileNm, chMailTemplate, &cfg.netArea);
  335.   zero_struct(cmds);
  336.   cmds.command = CHECK_MAIL;
  337.   if (!sendNetCommand(&cmds, "check mail"))
  338.     {
  339.     return;
  340.  
  341.     }
  342.   if (ITL_Receive(fileNm, FALSE, TRUE, putFLChar, fclose) == ITL_SUCCESS)
  343.   checkNegMail = TRUE;        /* Call readNegMail() later */
  344.  
  345.   }
  346. /*
  347. * readNegMail()
  348. *
  349. * This function reads and processes negative acks.
  350. */
  351. void readNegMail(char talk)
  352.   {
  353.   label author, target, context;
  354.   int whatLog;
  355.   int  sigChar;
  356.   SYS_FILE fileNm;
  357.   extern char *READ_ANY;
  358.   makeSysName(fileNm, chMailTemplate, &cfg.netArea);
  359.   if ((netMisc = safeopen(fileNm, READ_ANY)) == NULL)
  360.     {
  361.     if (talk) no_good("Couldn't open negative ack file from %s.", FALSE);
  362.     return ;
  363.  
  364.     }
  365.   getRoom(MAILROOM);
  366.   sigChar = fgetc(netMisc);
  367.   while (sigChar != NO_ERROR && sigChar != EOF && sigChar != EOF)
  368.     {
  369.     ZeroMsgBuffer(&msgBuf);
  370.     strCpy(msgBuf.mbauth, "Citadel");
  371.     getMsgStr(getNetChar, author, NAMESIZE);
  372.     getMsgStr(getNetChar, target, NAMESIZE);
  373.     getMsgStr(getNetChar, context, NAMESIZE);
  374.     switch (sigChar)
  375.       {
  376.       case NO_RECIPIENT:
  377.       strCpy(msgBuf.mbto, author);
  378.       if ((whatLog = PersonExists(author)) >= 0 &&
  379.       whatLog < cfg.MAXLOGTAB)
  380.         {
  381.         sPrintf(msgBuf.mbtext,
  382.         "Your netMail to '%s' (%s) failed because there is no such recipient on %s.",
  383.         target, context, callerName);
  384.         putMessage(&logBuf);
  385.         break;
  386.  
  387.         }
  388.       case UNKNOWN:
  389.       ZeroMsgBuffer(&msgBuf);
  390.       sPrintf(msgBuf.mbtext,
  391.       "Unknown problems with netMail: author=-%s-, target=-%s-, context=-%s-.  System was %s.",
  392.       author, target, context, netBuf.netName);
  393.       netResult(msgBuf.mbtext);
  394.       break;
  395.       case BAD_FORM:
  396.       sPrintf(msgBuf.mbtext, "Bad netMail sent to %s.", callerName);
  397.       netResult(msgBuf.mbtext);
  398.       break;
  399.       default:
  400.       sPrintf(msgBuf.mbtext, "Bad sigChar=%d.", sigChar);
  401.       netResult(msgBuf.mbtext);
  402.       break;
  403.  
  404.       }
  405.     sigChar = fgetc(netMisc);
  406.  
  407.     }
  408.   fclose(netMisc);
  409.   unlink(fileNm);
  410.  
  411.   }
  412. /*
  413. * sendSharedRooms()
  414. *
  415. * This sends all shared rooms to receiver.
  416. */
  417. static int SendRoom(SharedRoom *room, int system, int index, int roomslot,void *d);
  418. void sendSharedRooms()
  419.   {
  420.   SendFastTransfer();
  421.   EachSharedRoom(thisNet, SendRoom, SendVirtualRoom, NULL);
  422.  
  423.   }
  424. /*
  425. * EachSharedRoom()
  426. *
  427. * This does something for each shared room.
  428. */
  429. void EachSharedRoom(int system,
  430. int (*func)(SharedRoom *room, int system, int index, int roomslot, void *d),
  431. int (*virtfunc)(VirtualRoom *room, int sys, int index, int which, void *d),
  432. void *data)
  433.   {
  434.   int rover;
  435.   if (!netTab[system].ntflags.in_use) return;
  436.   if (func != NULL)
  437.     {
  438.     for (rover = 0; rover < SHARED_ROOMS; rover++)
  439.       {
  440.       if (isSharedRoom(system, rover) && roomValidate(system, rover))
  441.         {
  442.         if ((*func)(netTab[system].netTRooms + rover, system,
  443.         rover, netTabRoomSlot(system, rover), data) == ERROR)
  444.         return;
  445.  
  446.         }
  447.  
  448.       }
  449.  
  450.     }
  451.   if (virtfunc != NULL) DoVirtuals(system, virtfunc, data);
  452.  
  453.   }
  454. /*
  455. * Addressing()
  456. *
  457. * This function is responsible for deciding what sort of addressing or routing
  458. * flags should be checked for, and if the room should be sent if we are in
  459. * a network session.
  460. */
  461. void Addressing(int system, int index, char *commnd, char **send1, char **send2,
  462. char **send3, char **name, char *doit)
  463.   {
  464.   extern char *R_SH_MARK, *NON_LOC_NET, *LOC_NET;
  465.   /*
  466.   * This is more than just a trivial efficiency.  This routine can be called
  467.   * indirectly by the room editing functions.  If this happens then the
  468.   * getRoom() call would overwrite the roomBuf being used for editing.
  469.   * Therefore, we can only read in roomBuf if we don't have the right one
  470.   * in place, otherwise we lose what we just changed.
  471.   */
  472.   if (thisRoom != netTabRoomSlot(system, index))
  473.   getRoom(netTabRoomSlot(system, index));
  474.   *doit = TRUE;
  475.   *send1 = R_SH_MARK;
  476.   *send2 = *send3 = "guh";
  477.   switch (roomBuf.rbShareType)
  478.     {
  479.     case PEON:
  480.     *commnd = NET_ROOM;
  481.     *name = "Peon";
  482.     break;
  483.     case REG_HOST:          /* obsolete */
  484.     case BACKBONE:
  485.     switch (GetMode(netTab[system].netTRooms[index].mode))
  486.       {
  487.       case PEON:
  488.       *commnd = NET_ROOM;
  489.       *send2 = NON_LOC_NET;
  490.       *name = "Peon";
  491.       break;
  492.       case ACTIVE_BACKBONE:
  493.       case REG_HOST:
  494.       if (netTab[system].ntflags.local)
  495.       *commnd = NET_ROOM;
  496.       else
  497.       *commnd = NET_ROUTE_ROOM;
  498.       *send2 = NON_LOC_NET;
  499.       *send3 = LOC_NET;
  500.       *name = "Backbone - Active";
  501.       break;
  502.       case PASS_BACKBONE:
  503.       if (!netTab[system].ntflags.local && !inReceive) *doit = FALSE;
  504.       else if (netTab[system].ntflags.local)
  505.       *commnd = NET_ROOM;
  506.       else
  507.       *commnd = NET_ROUTE_ROOM;
  508.       *send2 = NON_LOC_NET;
  509.       *send3 = LOC_NET;
  510.       *name = "Backbone - Passive";
  511.       break;
  512.       default: crashout("shared rooms: #2");
  513.  
  514.       }
  515.     break;
  516.     default: crashout("shared rooms: #1");
  517.  
  518.     }
  519.   }
  520. /*
  521. * SendRoom()
  522. *
  523. * Sends a room to the receiving system during netting.  It returns ERROR if
  524. * carrier etc is lost.
  525. */
  526. static int SendRoom(SharedRoom *room, int system, int index, int roomslot,
  527. void *d)
  528.   {
  529.   char cmd;
  530.   char doit, *s1, *s2, *s3, *name;
  531.   if (!gotCarrier())
  532.     {
  533.     modStat = haveCarrier = FALSE;
  534.     return ERROR;
  535.  
  536.     }
  537.   Addressing(system, index, &cmd, &s1, &s2, &s3, &name, &doit);
  538.   if (doit && SR_Sent[index] != 1 &&
  539.   (roomTab[roomslot].rtlastNet >  netBuf.netRooms[index].lastMess ||
  540.   GetFA(room->mode) ||
  541.   (roomTab[roomslot].rtShareType == BACKBONE &&
  542.   GetMode(room->mode) != PEON &&  !netBuf.nbflags.local))
  543.   )
  544.     {
  545.     /**
  546.     if (cfg.BoolFlags.debug)
  547.     splitF(netLog, "Why: %s && %d[%d] && ( %ld > %ld || %d || ( %s BACKBONE && %s PEON && %s LOCAL))\n",
  548.     (doit ? "TRUE":"FALSE"), SR_Sent[index],index,
  549.     roomTab[roomslot].rtlastNet,netBuf.netRooms[index].lastMess,GetFA(room->mode),
  550.     (roomTab[roomslot].rtShareType == BACKBONE ? "" : "Not"),
  551.     (GetMode(room->mode) != PEON ? "Not" : ""),
  552.     (netBuf.nbflags.local ? "" : "NOT"));
  553.     **/
  554.     ITL_optimize(TRUE);
  555.     findAndSend((RecMassTransfer || chkNeedsProcessing(index)) ?
  556.     NET_ROOM : cmd, s1, s2, s3, index,
  557.     RoomSend, roomTab[roomslot].rtname, RoomReceive);
  558.  
  559.     }
  560.   return TRUE;
  561.  
  562.   }
  563. /*
  564. * findAndSend()
  565. *
  566. * This function manages sending a room (virtual or normal) to the receiver,
  567. * handling both normal and route rooms, via function pointers.
  568. */
  569. void  findAndSend(int commnd, char *send1, char *send2, char *send3, int rover,
  570. int (*MsgSender)(int r, char *d1, char *d2, char *d3),
  571. label roomName, int (*MsgReceiver)(int r, char y))
  572.   {
  573.   struct cmd_data cmds;
  574.   extern MessageBuffer tempMess;
  575.   int  tempcount;
  576.   extern char *netRoomTemplate, *WRITE_ANY;
  577.   if (!gotCarrier()) return;
  578.   zero_struct(cmds);
  579.   cmds.command = commnd;
  580.   strCpy(cmds.fields[0], roomName);
  581.   if (commnd != ERROR)
  582.   if (!sendNetCommand(&cmds, "shared rooms"))
  583.     {
  584.     if (commnd == NET_ROUTE_ROOM)
  585.       {
  586.       findAndSend(NET_ROOM, send1, send2, send3, rover, MsgSender,
  587.       roomName, MsgReceiver); /* time to recurse */
  588.       return ;
  589.  
  590.       }
  591.     else
  592.       {
  593.       sPrintf(tempMess.mbtext, "%%s reports: %s (%s) commnd=%d", RecBuf+1,
  594.       roomName,commnd);
  595.       no_good(tempMess.mbtext, FALSE);
  596.       return ;
  597.  
  598.       }
  599.  
  600.     }
  601.   if (!ITL_SendMessages())
  602.     {
  603.     no_good("Couldn't start WC for room sharing: %s",
  604.     FALSE);
  605.     return;
  606.  
  607.     }
  608.   tempcount = (*MsgSender)(rover, send1, send2, send3);
  609.   ITL_StopSendMessages();
  610.   if( logNetResults && netDebug )
  611.     {
  612.     if( EncCount > 0)
  613.       splitF(netLog, "(%d) (%ld => %ld bytes)\n", tempcount, EncCount, ByteCount);
  614.     else splitF(netLog,"\n");
  615.     };
  616.   if (commnd == NET_ROUTE_ROOM)
  617.     {
  618.     (*MsgReceiver)(rover, FALSE);
  619.  
  620.     }
  621.  
  622.   }
  623. /*
  624. * RoomReceive()
  625. *
  626. * This function receives messages for a room.
  627. */
  628. int RoomReceive(int rover, char ReplyFirst)
  629.   {
  630.   recNetMessages(rover, roomBuf.rbname, netRoomSlot(rover), FALSE);
  631.   return 0;
  632.  
  633.   }
  634. /*
  635. * RoomSend()
  636. *
  637. * This function sends messages for a room.
  638. */
  639. int RoomSend(int rover, char *send1, char *send2, char *send3)
  640.   {
  641.   extern char PrTransmit;
  642.   char work[10];
  643.   int MsgCount = 0;
  644.   if( logNetResults && netDebug )splitF(netLog, "Sending %s ", roomBuf.rbname);
  645.   zero_struct(NetStyle);
  646.   NetStyle.addr1 = send1;
  647.   NetStyle.addr2 = send2;
  648.   NetStyle.addr3 = send3;
  649.   if (GetFA(netBuf.netRooms[rover].mode))
  650.     {
  651.     sPrintf(work, CACHE_END_NAME, netRoomSlot(rover));
  652.     if (SendPrepAsNormal(work, &MsgCount))
  653.     UnSetFA(netBuf.netRooms[rover].mode);
  654.  
  655.     }
  656.   NetStyle.sendfunc = sendITLchar;
  657.   /*   PrTransmit = FALSE; */
  658.   MsgCount += showMessages(NEWoNLY, FALSE, netBuf.netRooms[rover].lastMess,
  659.   NetRoute);
  660.   if (TrError == TRAN_SUCCESS)
  661.     {
  662.     SetHighValues(rover);
  663.     SR_Sent[rover] = 1;
  664.  
  665.     }
  666.   else
  667.     {
  668.     MsgCount = 0;
  669.  
  670.     }
  671.   return MsgCount;
  672.  
  673.   }
  674. /*
  675. * SetHighValues()
  676. *
  677. * This function sets the high message sent for a normal shared room after a
  678. * successful net session has (apparently) taken place.
  679. */
  680. void SetHighValues(int rover)
  681.   {
  682.   if (NetStyle.HiSent == 0l)
  683.     {
  684.     NetStyle.HiSent =
  685.     max(netBuf.netRooms[rover].lastMess,roomTab[thisRoom].rtlastMessage);
  686.  
  687.     }
  688.   netBuf.netRooms[rover].lastMess = NetStyle.HiSent;
  689.   netTab[thisNet].netTRooms[rover].lastMess = NetStyle.HiSent;
  690.  
  691.   }
  692. /*
  693. * SendPrepAsNormal()
  694. *
  695. * This function sends files prepared for cache sending as normal message files,
  696. * instead.
  697. */
  698. char SendPrepAsNormal(char *work, int *MsgCount)
  699.   {
  700.   char tempNm[3*NAMESIZE];
  701.   NetCacheName(tempNm, thisNet, work);
  702.   if ((netMisc = safeopen(tempNm, READ_ANY)) != NULL)
  703.     {
  704.     while (getMessage(getNetChar, TRUE, TRUE, TRUE))
  705.       {
  706.       (*MsgCount)++;
  707.       prNetStyle(0, sendITLchar, FALSE, "");
  708.  
  709.       }
  710.     fclose(netMisc);
  711.  
  712.     }
  713.   if (gotCarrier())
  714.     {
  715.     unlink(tempNm);
  716.     return TRUE;
  717.  
  718.     }
  719.   return FALSE;
  720.  
  721.   }
  722. /*
  723. * NetRoute()
  724. *
  725. * This is a worker function, returns TRUE if message sent.
  726. */
  727. char NetRoute(int status)
  728.   {
  729.   long temp;
  730.   if ((strncmp(msgBuf.mbaddr, NetStyle.addr1, strLen(NetStyle.addr1))
  731.   == SAMESTRING  ||
  732.   strncmp(msgBuf.mbaddr, NetStyle.addr2, strLen(NetStyle.addr2))
  733.   == SAMESTRING  ||
  734.   strncmp(msgBuf.mbaddr, NetStyle.addr3, strLen(NetStyle.addr3))
  735.   == SAMESTRING) &&
  736.   RoutePath(LOC_NET, msgBuf.mbaddr)     != thisNet       &&
  737.   RoutePath(NON_LOC_NET, msgBuf.mbaddr) != thisNet)
  738.     {
  739.     prNetStyle(FALSE, NetStyle.sendfunc, TRUE, "");
  740.     temp = atol(msgBuf.mbId);
  741.     NetStyle.HiSent = max(NetStyle.HiSent,temp);
  742.     return TRUE;
  743.  
  744.     }
  745.   return FALSE;
  746.  
  747.   }
  748. /*
  749. * RoutePath()
  750. *
  751. * This function returns the number of the node that routed this msg to here.
  752. * If the msg was not routed in from a BackBone, then return ERROR, which will
  753. * never match another node's #.
  754. *
  755. * 88Oct13: Now simply check for msg origin, assume if one exists that it
  756. * should be checked.  Don't remember why it is restricted to only
  757. * BACKBONE-routed msgs.  Doesn't seem necessary.
  758. */
  759. int RoutePath(char *rp, char *str)
  760.   {
  761.   if (strncmp(rp, str, strLen(rp)) == SAMESTRING)
  762.     {
  763.     if (strLen(str) != strLen(rp)) /* prevent return of 0 */
  764.     return atoi(str + 2);
  765.  
  766.     }
  767.   return ERROR;
  768.  
  769.   }
  770. /*
  771. * doSendFiles()
  772. *
  773. * This function will send files to a victim.
  774. */
  775. void doSendFiles()
  776.   {
  777.   extern char       *READ_ANY;
  778.   struct   fl_send  theFiles;
  779.   SYS_FILE          sdFile;
  780.   char              temp[8];
  781.   FILE              *fd;
  782.   ITL_optimize(FALSE);    /* try for better protocol */
  783.   sPrintf(temp, "%d.sfl", thisNet);
  784.   makeSysName(sdFile, temp, &cfg.netArea);
  785.   if ((fd = safeopen(sdFile, READ_ANY)) == NULL)
  786.     {
  787.     sPrintf(msgBuf.mbtext, "Couldn't open send file %s for %s!",
  788.     sdFile,netBuf.netName);
  789.     netResult(msgBuf.mbtext);
  790.     netBuf.nbflags.send_files = FALSE;
  791.  
  792.     }
  793.   else
  794.     {
  795.     while (getSLNet(theFiles, fd) && haveCarrier)
  796.       {
  797.       sysSendFiles(&theFiles);
  798.  
  799.       }
  800.     fclose(fd);
  801.     if (haveCarrier)
  802.       {
  803.       /* if no carrier, was an error during transmit */
  804.       unlink(sdFile);
  805.       netBuf.nbflags.send_files = FALSE;
  806.  
  807.       }
  808.  
  809.     }
  810.  
  811.   }
  812. /*
  813. * netSendFile()
  814. *
  815. * This function will send a file to another system via net.
  816. */
  817. void netSendFile(DirEntry *fn)
  818.   {
  819.   extern char     *READ_ANY;
  820.   struct cmd_data cmds;
  821.   char            mess[140];
  822.   if (!gotCarrier()) return ;
  823.   if( logNetResults && netDebug )splitF(netLog, "Send File: %s\n", fn->unambig);
  824.   zero_struct(cmds);
  825.   cmds.command = SEND_FILE;
  826.   strCpy(cmds.fields[0], fn->unambig);
  827.   sPrintf(cmds.fields[1], "%ld", (fn->FileSize + SECTSIZE - 1) / SECTSIZE);
  828.   sPrintf(cmds.fields[2], "%ld", fn->FileSize);
  829.   if (!sendNetCommand(&cmds, "send file"))
  830.     {
  831.     if (haveCarrier)
  832.       {
  833.       strCpy(mess, "%s reports: ");
  834.       strCat(mess, RecBuf + 1);
  835.       no_good(mess, FALSE);
  836.  
  837.       }
  838.  
  839.     }
  840.   else
  841.     {
  842.     SendHostFile(fn->unambig);
  843.     if (haveCarrier)
  844.       {
  845.       sPrintf(msgBuf.mbtext, "%s sent to %s.", fn->unambig,
  846.       netBuf.netName);
  847.       netResult(msgBuf.mbtext);
  848.  
  849.       }
  850.  
  851.     }
  852.  
  853.   }
  854. extern FILE *upfd;
  855. /*
  856. * askFiles()
  857. *
  858. * This function will ask for file(s) from caller.
  859. */
  860. static void fl_req_free(struct fl_req *d);
  861. void askFiles()
  862.   {
  863.   label    data2;
  864.   SYS_FILE dataFl;
  865.   char     mess[130];
  866.   char     ambiguous;
  867.   int      result = ITL_SUCCESS;
  868.   FILE     *temp;
  869.   struct   cmd_data cmds;
  870.   struct   fl_req file_data, *list;
  871.   SListBase Failed =
  872.     {
  873.     NULL, NULL, NULL, fl_req_free, NULL
  874.  
  875.     };
  876.   extern char *READ_ANY, *WRITE_ANY;
  877.   if (!gotCarrier())
  878.     {
  879.     modStat = haveCarrier = FALSE;
  880.     return ;
  881.  
  882.     }
  883.   sPrintf(data2, "%d.rfl", thisNet);
  884.   makeSysName(dataFl, data2, &cfg.netArea);
  885.   temp = safeopen(dataFl, READ_ANY);
  886.   if (temp == NULL)
  887.     {
  888.     no_good("Couldn't open room request file for %s", FALSE);
  889.     netBuf.nbflags.room_files = FALSE;
  890.  
  891.     }
  892.   else
  893.     {
  894.     ITL_optimize(FALSE);    /* try for better protocol */
  895.     while ( result == ITL_SUCCESS &&
  896.     fread(&file_data, sizeof (file_data), 1, temp) == 1 &&
  897.     gotCarrier() && result == ITL_SUCCESS)
  898.       {
  899.       if (netSetNewArea(&file_data.flArea))
  900.         {
  901.         zero_struct(cmds);
  902.         ambiguous = !(strchr(file_data.roomfile, '*') == NULL &&
  903.         strchr(file_data.roomfile, '?') == NULL);
  904.         cmds.command = (!ambiguous) ? R_FILE_REQ : A_FILE_REQ;
  905.         strCpy(cmds.fields[0], file_data.room);
  906.         strCpy(cmds.fields[1], file_data.roomfile);
  907.         if( logNetResults )splitF(netLog, "Requesting %s in %s\n", file_data.roomfile,
  908.         file_data.room);
  909.         if (!sendNetCommand(&cmds,
  910.         (!ambiguous) ? "single file request" :
  911.         "multiple file request"))
  912.           {
  913.           sPrintf(mess, "%%s reports %s for file %s in %s.", RecBuf+1,
  914.           file_data.roomfile, file_data.room);
  915.           no_good(mess, FALSE);
  916.  
  917.           }
  918.         else
  919.           {
  920.           if (ambiguous)
  921.           result = multiReceive(&file_data);
  922.           else
  923.             {
  924.             if ((result = ITL_Receive(file_data.filename, FALSE,
  925.             TRUE, putFLChar, fclose)) == ITL_SUCCESS)
  926.               {
  927.               sPrintf(msgBuf.mbtext,
  928.               "File '%s' received from %s (stored in directory %s).",
  929.               file_data.filename, netBuf.netName,
  930.               prtNetArea(&file_data.flArea));
  931.               netResult(msgBuf.mbtext);
  932.  
  933.               }
  934.  
  935.             }
  936.  
  937.           }
  938.  
  939.         }
  940.       homeSpace();
  941.  
  942.       }
  943.     if (gotCarrier())
  944.       {
  945.       fclose(temp);
  946.       unlink(dataFl);
  947.       netBuf.nbflags.room_files = FALSE;
  948.  
  949.       }
  950.     else
  951.       {
  952.       haveCarrier = modStat = FALSE;
  953.       /* Now find out what we didn't get and set up a new request queue */
  954.       do
  955.         {
  956.         /* use do loop to get the one it failed in */
  957.         list = GetDynamic(sizeof *list);
  958.         copy_struct(file_data, (*list));
  959.         AddData(&Failed, list, NULL, FALSE);
  960.  
  961.         }
  962.       while (fread(&file_data, sizeof (file_data), 1, temp) == 1);
  963.       fclose(temp);
  964.       unlink(dataFl);
  965.       upfd = fopen(dataFl, WRITE_ANY);
  966.       KillList(&Failed);
  967.       fclose(upfd);
  968.  
  969.       }
  970.  
  971.     }
  972.  
  973.   }
  974. /*
  975. * fl_req_free()
  976. *
  977. * This will write and free a file request.
  978. */
  979. static void fl_req_free(struct fl_req *d)
  980.   {
  981.   if (upfd != NULL) fwrite(d, sizeof *d, 1, upfd);
  982.   free(d);
  983.  
  984.   }
  985. /*
  986. * multiReceive()
  987. *
  988. * This function will receive multiple files.
  989. */
  990. char multiReceive(struct fl_req *file_data)
  991.   {
  992.   char        first = 1;
  993.   extern char *WRITE_ANY;
  994.   sPrintf(msgBuf.mbtext,
  995.   "Following files received from %s in response to request for %s from %s: ",
  996.   netBuf.netName, file_data->roomfile, file_data->room);
  997.   do
  998.     {
  999.     if (ITL_Receive(NULL, FALSE, TRUE, putFLChar, fclose) != ITL_SUCCESS ||
  1000.     !gotCarrier()) return ITL_BAD_TRANS;
  1001.     if (RecBuf[0] == 0)
  1002.       {
  1003.       /* Last file name       */
  1004.       sPrintf(lbyte(msgBuf.mbtext), " (stored in directory %s).",
  1005.       prtNetArea(&file_data->flArea));
  1006.       netResult(msgBuf.mbtext);
  1007.       return ITL_SUCCESS;
  1008.  
  1009.       }
  1010.     if (!first)
  1011.     strCat(msgBuf.mbtext, ", ");
  1012.     else
  1013.     first = FALSE;
  1014.     strCat(msgBuf.mbtext, RecBuf);
  1015.     if (ITL_Receive(RecBuf, FALSE, TRUE, putFLChar, fclose) != ITL_SUCCESS
  1016.     || !gotCarrier()) return ITL_BAD_TRANS;
  1017.  
  1018.     }
  1019.   while (1);
  1020.   return ITL_SUCCESS;
  1021.  
  1022.   }
  1023. /*
  1024. * sendNetCommand()
  1025. *
  1026. * This sends a command to the receiver.
  1027. */
  1028. char sendNetCommand(struct cmd_data *cmds, char *error)
  1029.   {
  1030.   char errMsg[100];
  1031.   int  count;
  1032.   if (cfg.BoolFlags.debug && netDebug )
  1033.     {
  1034.     splitF(netLog, "Sending Command: %d\n", cmds->command);
  1035.     splitF(netLog, " Field[0]: %s\n", cmds->fields[0]);
  1036.     splitF(netLog, " Field[1]: %s\n", cmds->fields[1]);
  1037.     splitF(netLog, " Field[2]: %s\n", cmds->fields[2]);
  1038.     splitF(netLog, " Field[3]: %s\n", cmds->fields[3]);
  1039.     };
  1040.  
  1041.   if (!ITL_Send(STARTUP))
  1042.     {
  1043.     sPrintf(errMsg, "Link failure for %s (system: %%s).", error);
  1044.     if (cmds->command != HANGUP) no_good(errMsg, TRUE);
  1045.     killConnection();
  1046.     return FALSE;
  1047.  
  1048.     }
  1049.   sendITLchar(cmds->command);
  1050.   for (count = 0; count < 4; count++)
  1051.     {
  1052.     if (cmds->fields[count][0])
  1053.       {
  1054.       mTrPrintf("%s", cmds->fields[count]);
  1055.  
  1056.       }
  1057.  
  1058.     }
  1059.   sendITLchar(0);
  1060.   ITL_Send(FINISH);
  1061.   if (cmds->command == HANGUP && !inReceive) return TRUE;
  1062.   ITL_Receive(NULL, FALSE, TRUE, putFLChar, fclose);
  1063.   if (RecBuf[0] == BAD || !gotCarrier()) return FALSE;
  1064.   return TRUE;
  1065.  
  1066.   }
  1067. /*
  1068. * sendHangUp()
  1069. *
  1070. * This sends the hangup command to receiver.
  1071. */
  1072. void sendHangUp()
  1073.   {
  1074.   struct cmd_data cmds;
  1075.   if (!gotCarrier())
  1076.     {
  1077.     modStat = haveCarrier = FALSE;
  1078.     return ;
  1079.  
  1080.     }
  1081.   zero_struct(cmds);
  1082.   cmds.command = HANGUP;
  1083.   sendNetCommand(&cmds, "HANGUP");
  1084.  
  1085.   }
  1086. /*
  1087. * no_good()
  1088. *
  1089. * This handles error messages when something really bad happens.
  1090. */
  1091. void no_good(char *str, char hup)
  1092.   {
  1093.   sPrintf(msgBuf.mbtext, str, netBuf.netName);
  1094.   if (hup)
  1095.     {
  1096.     killConnection();
  1097.  
  1098.     }
  1099.   netResult(msgBuf.mbtext);
  1100.  
  1101.   }
  1102. /*
  1103. * s_m_n()
  1104. *
  1105. * This sends mail normal (non-route mail).
  1106. */
  1107. int s_m_n()
  1108.   {
  1109.   FILE     *ptrs;
  1110.   label    fntemp;
  1111.   SYS_FILE fn;
  1112.   int      messCount = 0;
  1113.   struct   netMLstruct buf;
  1114.   extern char *READ_ANY;
  1115.   sPrintf(fntemp, "%d.ml", thisNet);
  1116.   makeSysName(fn, fntemp, &cfg.netArea);
  1117.   if ((ptrs = safeopen(fn, READ_ANY)) == NULL)
  1118.     {
  1119.     if (netBuf.nbflags.normal_mail)
  1120.       {
  1121.       sPrintf(msgBuf.mbtext, "No mail file to send to %s?",
  1122.       netBuf.netName);
  1123.       netResult(msgBuf.mbtext);
  1124.  
  1125.       }
  1126.     return 0;
  1127.  
  1128.     }
  1129.   while (getMLNet(ptrs, buf) && TrError == TRAN_SUCCESS)
  1130.     {
  1131.     if (findMessage(buf.ML_loc, buf.ML_id, TRUE))
  1132.       {
  1133.       if (netDebug)
  1134.         {
  1135.         MessageBuffer *msg = &msgBuf;
  1136.         splitF(netLog, "Message %6d Sector ID: %6d Author:%s\n",  msg->mbheadChar, msg->mbheadSector, msg->mbauth);
  1137.         splitF(netLog, "Date:%20s Time:%20s Local Id:%20s\n",  msg->mbdate,  msg-> mbtime,  msg-> mbId);
  1138.         splitF(netLog, "Human:%20s ID:%20s Room:%20s\n",  msg->mboname,  msg->mborig,  msg->mbroom);
  1139.         splitF(netLog, "Origin:%20s To:%20s\n",  msg->mbsrcId,  msg->mbto);
  1140.         splitF(netLog, "Route:%s\n", msg->mbaddr);
  1141.         splitF(netLog, "OtherNet:%s\n",msg->mbOther);
  1142.         splitF(netLog, "reply:%20s Domain:%20s\n",msg->mbreply, msg->mbdomain);
  1143.         }
  1144.       prNetStyle(0, sendITLchar, TRUE, netBuf.netName);
  1145.       messCount++;
  1146.  
  1147.       }
  1148.  
  1149.     }
  1150.   fclose(ptrs);
  1151.   if (TrError == TRAN_SUCCESS)
  1152.     {
  1153.     unlink(fn);
  1154.     return messCount;
  1155.  
  1156.     }
  1157.   killConnection();
  1158.   if( logNetResults )splitF(netLog, "\nFailed transferring mail!\n");
  1159.   return 0;
  1160.  
  1161.   }
  1162. /*
  1163. * SendHostFile()
  1164. *
  1165. * This function will send a file to the other system.
  1166. */
  1167. void SendHostFile(char *fn)
  1168.   {
  1169.   int  success;
  1170.   FILE *fd;
  1171.   extern int errno;
  1172.   extern char *READ_ANY;
  1173.   success = ((fd = safeopen(fn, READ_ANY)) != NULL);
  1174.   if (ITL_Send(STARTUP))
  1175.     {
  1176.     if (!success) mTrPrintf("System error failure, this is a bogus file.");
  1177.     else
  1178.       {
  1179.       SendThatDamnFile(fd, sendITLchar);
  1180.  
  1181.       }
  1182.     ITL_Send(FINISH);
  1183.  
  1184.     }
  1185.   if (!success)
  1186.     {
  1187.     sPrintf(msgBuf.mbtext, "System error! Couldn't open %s for %s, errno %d.",
  1188.     fn, netBuf.netName, errno);
  1189.     netResult(msgBuf.mbtext);
  1190.  
  1191.     }
  1192.  
  1193.   }
  1194.